import wx
from TrainModel import load_model
import Dict

result = []


def get_questions(filepath):
    lis = []
    with open(filepath, 'r', encoding='utf-8')as file:
        while True:
            temp = file.readline()
            if not temp:
                break
            lis.append(temp.split())
    return lis


def predict_():
    lis_turn = []
    m = load_model('model.m')
    loc_count = 0
    for i in result:
        loc_count += 1
        if i not in [str(j) for j in range(1, 300)]:
            lis_turn.append(Dict.d[i])
        else:
            if loc_count == 3:
                lis_turn.append(int(i)/100)
            else:
                lis_turn.append(int(i))
    result.clear()
    # print(lis_turn)
    return Dict.d[m.predict(lis_turn)[0]]


class Win(wx.Frame):
    __app = wx.App()
    __lis = []

    def __init__(self, filepath):
        wx.Frame.__init__(self, None, -1, '肥胖预测', size=(620, 600),
                          style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX)
        self.Center()
        panel = wx.Panel(self)
        panel.SetBackgroundColour('white')
        questions = get_questions(filepath)
        grid = wx.GridBagSizer(hgap=3, vgap=3)
        row_count, col_count, count = 0, 1, 63
        for i in questions:
            if len(i) > 1:
                self.__radiobox = wx.RadioBox(panel, -1, i[0], choices=i[1:], style=wx.RA_SPECIFY_COLS)
                grid.Add(self.__radiobox, pos=(row_count, col_count), flag=wx.EXPAND)
                self.__lis.append(self.__radiobox)
            else:
                wx.StaticText(panel, -1, label=i[0], size=(110, 50), pos=(85, count))
                self.__combol = wx.ComboBox(panel, -1, choices=[str(j) for j in range(1, 300)], size=(60, 30),
                                            style=wx.CB_READONLY)
                self.__combol.SetValue('1')
                grid.Add(self.__combol, pos=(row_count, col_count))
                self.__lis.append(self.__combol)
                count += 60
            row_count += 1
            if row_count == 9:
                row_count = 0
                col_count += 1
        panel.SetSizer(grid)
        self.__button = wx.Button(panel, -1, label='确定', size=(70, 50), pos=(460, 460))
        self.Bind(wx.EVT_BUTTON, self.__get_choice, self.__button)
        self.__result_label = wx.StaticText(panel, -1, size=(110, 50), pos=(330, 460))
        self.__result_label.SetForegroundColour('red')

    def mainloop(self):
        self.Show()
        self.__app.MainLoop()

    def __get_choice(self, event):
        for i in self.__lis:
            result.append(i.GetStringSelection())
        text = predict_()
        self.__result_label.SetLabelText(text)


if __name__ == '__main__':
    win = Win('Questions.txt')
    win.mainloop()
